package cn.ldj.ztomcat.catalina;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.log.LogFactory;
import cn.hutool.system.SystemUtil;
import cn.ldj.ztomcat.Bootstrap;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;


public class Server {
    private Service service;
    public Server(){
        this.service = new Service(this);
    }

    public void start(){
        TimeInterval timeInterval = DateUtil.timer();
        logJVM();
        init();
        LogFactory.get().info("Server startup in {} ms",timeInterval.intervalMs());
    }

    private void init() {
        service.start();
    }


    private static void logJVM() {
        Map<String, String> infos = new LinkedHashMap<>();
        infos.put("Server version", "ldj DiyTomcat/1.0.1");
        long time = System.currentTimeMillis();
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日-hh时mm分ss秒");
        Date date = new Date(time);
        infos.put("Server built", format.format(date));
        infos.put("Server number", "1.0.1");
        infos.put("OS Name\t", SystemUtil.get("os.name"));
        infos.put("OS Version", SystemUtil.get("os.version"));
        infos.put("Architecture", SystemUtil.get("os.arch"));
        infos.put("Java Home", SystemUtil.get("java.home"));
        infos.put("JVM Version", SystemUtil.get("java.runtime.version"));
        infos.put("JVM Vendor", SystemUtil.get("java.vm.specification.vendor"));

        Set<String> keys = infos.keySet();
        for (String key : keys) {
            LogFactory.get().info(key + ":\t\t" + infos.get(key));
        }
    }

//    private static void handle200(Socket s, MyResponse response) throws IOException {
//        String contentType = response.getContentType();
//        String headText = Constant.response_head_202;
//        headText = StrUtil.format(headText, contentType);
//        byte[] head = headText.getBytes();
//
//        byte[] body = response.getBody();
//
//        byte[] responseBytes = new byte[head.length + body.length];
//        ArrayUtil.copy(head, 0, responseBytes, 0, head.length);
//        ArrayUtil.copy(body, 0, responseBytes, head.length, body.length);
//
//        OutputStream os = s.getOutputStream();
//        os.write(responseBytes);
//        s.close();
//    }
//
//    protected void handle404(Socket s, String uri) throws IOException {
//        OutputStream os = s.getOutputStream();
//        String responseText = StrUtil.format(Constant.textFormat_404, uri, uri);
//        responseText = Constant.response_head_404 + responseText;
//        byte[] responseByte = responseText.getBytes(StandardCharsets.UTF_8);
//        os.write(responseByte);
//    }
//
//    protected void handle500(Socket s, Exception e) {
//        try {
//            OutputStream os = s.getOutputStream();
//            StackTraceElement[] stes = e.getStackTrace();
//            StringBuffer sb = new StringBuffer();
//            sb.append(e.toString());
//            sb.append("\r\n");
//            for (StackTraceElement ste : stes) {
//                sb.append("\t");
//                sb.append(ste.toString());
//                sb.append("\r\n");
//            }
//
//            String msg = e.getMessage();
//
//            if (null != msg && msg.length() > 20)
//                msg = msg.substring(0, 19);
//
//            String text = StrUtil.format(Constant.textFormat_500, msg, e.toString(), sb.toString());
//            text = Constant.response_head_500 + text;
//            byte[] responseBytes = text.getBytes(StandardCharsets.UTF_8);
//            os.write(responseBytes);
//        } catch (IOException e1) {
//            e1.printStackTrace();
//        }
//    }
}
